package com.qianduo.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.qianduo.dao.IQianduoProductDao;
import com.qianduo.exception.DaoException;
import com.qianduo.po.QianduoProduct;
import com.qianduo.po.QianduoProductView;
import com.qianduo.util.HibernateUtil;
import common.page.PageList;
import common.page.PaginationQuery;
import common.util.StringUtil;

@SuppressWarnings({ "finally", "unchecked" })
public class QianduoProductDaoImpl extends HibernateUtil implements IQianduoProductDao{
	public void saveOrUpdateProduct(QianduoProduct po) throws DaoException{
		try{
			super.saveOrUpdate(po);
		}catch(Exception e){
			throw new DaoException(className+" saveOrUpdateProduct",e);
		}
	}
	
	public List<QianduoProductView> listProduct(Integer id,Integer type,Integer agencyID,String name,BigDecimal amount,Integer period,BigDecimal rate,String createDateStart,String createDateEnd,PageList pageList) throws DaoException{
		List<QianduoProductView> retList = null;
		try{
			Criteria ca	= getSessionFactory().getCurrentSession().createCriteria(QianduoProductView.class);
			if(null != id){
				ca.add(Restrictions.eq("id", id));
			}
			if(null != agencyID){
				ca.add(Restrictions.eq("agencyID", agencyID));
			}
			if(!StringUtil.isEmpty(name)){
				ca.add(Restrictions.like("name", "%"+name+"%"));
			}
			if(null != amount){
				ca.add(Restrictions.ge("maxAmount", amount));
				ca.add(Restrictions.le("minAmount", amount));
			}
			if(null != period){
				ca.add(Restrictions.ge("period", period));
			}
			if(null != rate){
				ca.add(Restrictions.ge("minRate", rate));
				ca.add(Restrictions.le("maxRate", rate));
			}
			if(!StringUtil.isEmpty(createDateStart)){
				ca.add(Restrictions.ge("createDate",createDateStart));
			}
			if(!StringUtil.isEmpty(createDateEnd)){
				ca.add(Restrictions.le("createDate",createDateEnd));
			}
			if(null != pageList){
				retList	= PaginationQuery.queryCriteria(ca, pageList);
			}else{
				retList	= ca.list();
			}
		}catch(Exception e){
			throw new DaoException(className+" listProduct",e);
		}finally{
			return retList;
		}
	}
}
